import { createRouter, createWebHashHistory } from "vue-router";
import loginView from "../views/loginView.vue";
import HotSelling from "@/views/HotSellingView.vue";
import GoodsDetails from "@/views/GoodsDetailsView.vue";
import allEvaluation from "@/views/allEvaluationView.vue";
import shoppingCart from "@/views/shoppingCartView.vue";

const routes = [
    // 登录页
    { path: "/", component: loginView },
    {
        // 主界面
        path: "/home",
        name: "home",
        redirect: "/home/homeindex",
        component: () => import("../views/homeView.vue"),
        children: [
            {
                // 首页
                path: "homeindex",
                component: () => import("../views/home-indexView.vue"),
            },
            {
                // 分类页
                path: "homesort",
                component: () => import("../views/home-sortView.vue"),
            },
            {
                // 我的页面
                path: "homemine",
                component: () => import("../views/home-mineView.vue"),
            },
            {
                // 未登录页面
                path: "homeunlogin",
                component: () => import("../views/home-unloginView.vue"),
            },
            {
                // 购物车
                path: "shoppingCart",
                name: "shoppingCart",
                component: shoppingCart,
                meta: {
                    requireLogin: true,
                },
            },
        ],
    },
    {
        // 搜索页
        path: "/search",
        component: () => import("../views/searchView.vue"),
        children: [
            {
                path: "searchindex",
                component: () => import("../views/search-indexView.vue"),
            },
            {
                path: "searchnotfound",
                component: () => import("../views/search-noneView.vue"),
            },
            {
                path: "searchfound",
                component: () => import("../views/search-foundView.vue"),
            },
        ],
    },
    {
        // 添加地址
        path: "/addaddress",
        component: () => import("../views/addAddressView.vue"),
    },
    {
        // 编辑地址
        path: "/editaddress",
        name: "editaddress",
        component: () => import("../views/editAddressView.vue"),
    },
    {
        // 结算成功
        path: "/paysuccess",
        component: () => import("../views/paySuccessView.vue"),
        meta: {
            requireLogin: true,
        },
    },
    {
        // 结算失败
        path: "/payfail",
        component: () => import("../views/payFailView.vue"),
    },
    {
        // 我的订单
        path: "/order",
        redirect: "/order/all/1",
        component: () => import("../views/orderView.vue"),
        children: [
            {
                path: "all/:status",
                name: "mineorder",
                component: () => import("../views/orderAll.vue"),
            },
        ],
    },
    {
        // 订单详情页
        path: "/orderdetail",
        name: "orderdetail",
        component: () => import("../views/orderDetailView.vue"),
    },
    {
        // 物流详情页
        path: "/logisticsdetails/:logistics",
        name: "logisticsdetails",
        component: () => import("../views/logisticsDetailsView.vue"),
    },
    {
        // 收货地址
        path: "/getgoodsaddress",
        component: () => import("../views/getGoodsAddressView.vue"),
    },
    {
        // 设置
        path: "/setting",
        component: () => import("../views/settingView.vue"),
    },
    {
        // 编辑个人信息
        path: "/editpersonal",
        component: () => import("../views/editPersonalInfoView.vue"),
    },
    {
        // 修改手机号
        path: "/editphone",
        component: () => import("../views/editPhoneNumView.vue"),
    },
    {
        // 隐私条款
        path: "/settingyinsi",
        component: () => import("../views/settingYinsiView.vue"),
    },
    // 商品热卖
    {
        path: "/HotSelling",
        name: "HotSelling",
        component: HotSelling,
    },
    // 商品详情
    {
        path: "/GoodsDetails",
        name: "GoodsDetails",
        component: GoodsDetails,
    },
    // 全部评价
    {
        path: "/allEvaluation",
        name: "allEvaluation",
        component: allEvaluation,
    },
    // 订单结算
    {
        path: "/orderSettlement",
        name: "orderSettlement",
        component: () => import("../views/orderSettlementView.vue"),
    },
    // 领券中心
    {
        path: "/CouponCenter",
        name: "CouponCenter",
        component: () => import("../views/CouponCenterView.vue"),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 设置路由守卫
router.beforeEach(function (to, from, next) {
    const requireLogin = to.meta.requireLogin;
    const token = localStorage.getItem("token");
    if (requireLogin) {
        if (token) {
            next();
        } else {
            if (from.path !== "/") {
                // 去提醒登录页 携带跳转前网址
                router.push("/home/homeunlogin/?redirect=" + to.path);
            }
        }
    } else {
        next();
    }
});

export default router;
